/*
 * 主罗拉编辑
 * 舞者无罪
 * 2014.12.04
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "mhbtn.h"
#include "mhedit.h"
#include "mhlabel.h"
#include "roll.h"
#include "com.h"
#include "keypane.h"
#include "mh/common.h"
#include "info.h"

#define ROLL_CAPTION_LEFT     (CX_EDGE_LEFT)
#define ROLL_CAPTION_TOP      (CY_EDGE_TOP + CX_EDGE_LEFT)
#define ROLL_CAPTION_WIDTH    (40)
#define ROLL_CAPTION_HEIGHT   (26)
#define ROLL_CAPTION_INTER    (CX_RAB_BMP - CX_EDGE_LEFT * 2) / 4
/*
 *
 */
static void CreateRollInterface(HWND hWnd)
{
    char str[32] = {0};
   CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_RAB_BMP - CX_ASK_BMP * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_RAB_BMP - CX_ASK_BMP * 3 - 8,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_TRUE]);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_RAB_BMP - CX_ASK_BMP * 4 - 8 * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_ASK]);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_FALSE,
        CX_RAB_BMP - CX_ASK_BMP * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_TRUE,
        CX_RAB_BMP - CX_ASK_BMP * 3 - 8,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_TRUE]);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_RAB_BMP - CX_ASK_BMP * 4 - 8 * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_ASK]);

    CreateWindow(MH_LABEL_NAME,
        MW_STOP_TORQUE_CAPTION,
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        CX_RAB_BMP / 2 - 106,
        ROLL_CAPTION_TOP,
        80,
        ROLL_CAPTION_HEIGHT,
        hWnd, MH_LABEL_BKGND_BLACK | MH_LABEL_TEXT_GREEN);
    sprintf(str, "%u", GetRollSpeedForMacStop(g_knit_data));
    CreateWindow(MH_EDIT_NAME,
        str,
        WS_CHILD | WS_VISIBLE,
        ID_EDIT,
        (CX_RAB_BMP - ROLL_CAPTION_WIDTH) / 2,
        ROLL_CAPTION_TOP,
        ROLL_CAPTION_WIDTH,
        ROLL_CAPTION_HEIGHT,
        hWnd, 0);
    int i = 0, j = 0;
    for(i = 0; i < 4; ++i)
    {
        for(j = 0; j < 8; ++j)
        {
            sprintf(str, "[%02d]", j * 4 + i + 1);
            CreateWindow(MH_BTN_NAME,
                str,
                WS_CHILD | WS_VISIBLE | BS_FLAT,
                ID_BUTTON1 + j * 4 + i,
                ROLL_CAPTION_LEFT + i * ROLL_CAPTION_INTER,
                ROLL_CAPTION_TOP + (j + 1) * ROLL_CAPTION_HEIGHT,
                ROLL_CAPTION_WIDTH,
                ROLL_CAPTION_HEIGHT,
                hWnd, MH_BTN_BKGND_BLACK | MH_BTN_TEXT_GREEN);
            sprintf(str, "%u", GetRollRunSpeed(g_knit_data, j * 4 + i));
            CreateWindow(MH_EDIT_NAME,
                str,
                WS_CHILD | WS_VISIBLE,
                IDC_EDIT1 + j * 4 + i,
                ROLL_CAPTION_LEFT + i * ROLL_CAPTION_INTER + ROLL_CAPTION_WIDTH + 10,
                ROLL_CAPTION_TOP + (j + 1) * ROLL_CAPTION_HEIGHT,
                ROLL_CAPTION_WIDTH,
                ROLL_CAPTION_HEIGHT,
                hWnd, 0);
        }
    }
}
/*
 *
 */
static void OnSetKnitValue(HWND hWnd, int nID)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
    uchar n = atoi(sc);
    if(nID == ID_EDIT)
    {
        data.rab.rsvd2[0] = n;
        data.roll.torque = TRUE;
    }
    else
    {
        SetRollRunSpeed(g_knit_data, nID - IDC_EDIT1, n);
        data.roll.change = TRUE;
    }
    SetWindowAdditionalData(hWnd, data.value);
}
/*
 * copy edits from setting just now
 */
static void CopyEdit(HWND hWnd)
{
    ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
    if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 32)
    {
        HWND hTmp = GetDlgItem(hWnd, nID);
        const char *sc = GetWindowCaption(hTmp);
        uchar n = atoi(sc);
        CopyKnitRollSpeed(g_knit_data, n);
        int i = 0;
        for(i = 0; i < 32; ++i)
        {
            hTmp = GetDlgItem(hWnd, IDC_EDIT1 + i);
            SetWindowCaption(hTmp, sc);
            InvalidateRect(hTmp, NULL, TRUE);
        }
    }
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    if(nID == ID_EDIT)
    {
        HWND hTmp = GetDlgItem(hWnd, nID);
        RECT rect;
        GetClientRect(hTmp, &rect);
        ClientToScreen(hTmp, &rect.left, &rect.top);
        ClientToScreen(hTmp, &rect.right, &rect.bottom);
        CreateKeyboardWindow(hWnd, rect, 0, 100, atoi(GetWindowCaption(hTmp)));
    }
    else if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 32)
    {
        HWND hTmp = GetDlgItem(hWnd, nID);
        RECT rect;
        GetClientRect(hTmp, &rect);
        ClientToScreen(hTmp, &rect.left, &rect.top);
        ClientToScreen(hTmp, &rect.right, &rect.bottom);
        CreateKeyboardWindow(hWnd, rect, 0, 100, atoi(GetWindowCaption(hTmp)));
    }
    else if(nID == ID_ESC)
    {
        WND_EXTRA_DATA data;
        data.value = GetWindowAdditionalData(hWnd);
        int ret1 = 0;
        if(data.roll.torque)
        {
            SetRollSpeedForMacStop(g_knit_data, data.rab.rsvd2[0]);
            ret1 = WriteMacFile(MAC_PARAM_FILE, g_knit_data);
        }
        int ret2 = 0;
        if(data.roll.change)
        {
            char sc[MH_FILE_NAME_MAX_LEN + sizeof(KNIT_FILE_DIR) + sizeof(".spd")] = { 0 };
            strcat(sc, KNIT_FILE_DIR);
            strcat(sc, GetKnitFileName(g_knit_data));
            strcat(sc, ".spd");
            ret2 = WriteSpeedFile(sc, g_knit_data);
            SendSpeedParam(g_knit_data);
        }
        if(data.roll.torque || data.roll.change)
        {
            if(!ret1 && !ret2)
            {
                MH_FILE_SYNC();
            }
            else
            {
                printf("Fialed to Write roll file: %d\n", ret1 * 10000 + ret2);
                AddSysLog(g_knit_data, LOG_TYPE_FILE, ret1 * 10000 + ret2);
                AfxInfoBox(hWnd, NULL, GetString(481));
            }
        }
        PostMessage (hWnd, MSG_CLOSE, 0, 0);
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_ENTER)
    {
        OnBtnCmd(hWnd, GetDlgCtrlID(GetFocusChild(hWnd)));
    }
    else if(key == SCANCODE_CURSORBLOCKRIGHT)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 32)
        {
            if(((nID - IDC_EDIT1) & 3) < 3)
            {
                ++nID;
                SetFocusChild(GetDlgItem(hWnd, nID));
            }
        }
    }
    else if(key == SCANCODE_CURSORBLOCKLEFT)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 32)
        {
            if((nID - IDC_EDIT1) & 3)
            {
                --nID;
                SetFocusChild(GetDlgItem(hWnd, nID));
            }
        }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 + 4 && nID < IDC_EDIT1 + 32)
        {
            nID -= 4;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
        else if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 4)
        {
            nID = ID_EDIT;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 28)
        {
            nID += 4;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
        else if(nID == ID_EDIT)
        {
            nID = IDC_EDIT1 + 2;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_C)
    {
        CopyEdit(hWnd);
    }
}
/*
*
*/
static int RollWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateRollInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 100;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_YELLOW(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting > 0)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup (hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_RAB]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_SETDATA:
        {
            OnSetKnitValue(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
*
*/
HWND CreateRollWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_RAB_BMP) / 2;
    int y = (CY_SCREEN - CY_RAB_BMP) / 2;

    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_ROLL_EDIT_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = RollWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_RAB_BMP;
    CreateInfo.by = y + CY_RAB_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
